package com.web.exiaodao.remote.controller;

import com.web.exiaodao.remote.service.ApiShopService;
import com.web.exiaodao.remote.vo.ShopVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.webstart.base.util.result.XaResult;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author zhanglin
 * @version V1.0
 * @Title: ApiProductTableController.java
 * @Package com.web.exiaodao.remote.controller
 * @Description: 商品表信息接口
 * @date 2016-11-02 17:35:34
 */
@Api(value = "shop", description = "商品接口", position = 10)
@Controller
@RequestMapping("/api/shop")
public class ApiShopController extends BaseController {

    private static final Logger LOGGER = Logger.getLogger(ApiShopController.class);
    @Autowired
    private ApiShopService shopService;

    /**
     * @return
     * @Title: findProductTableList
     * @Description: 查询所有商品
     */
    @ApiOperation(value = "查询所有商品", notes = "查询所有商品信息,当返回的code=1时，取出object进行显示,存放为数组对象")
    @ResponseBody
    @RequestMapping(value = "findShopList", method = RequestMethod.POST)
    public XaResult<List<ShopVo>> findShopList(
            @ApiParam("页号,字段名:nextPage,默认0,从第0页开始") @RequestParam(defaultValue = "0") Integer nextPage,
            @ApiParam("页长,字段名:pageSize,默认10") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("searchKey,搜索关键词") @RequestParam(required = false) String searchKey,
            @ApiParam("商品分类,字段名:shopCategoryId") @RequestParam(required = false) Long categoryType
    ) throws BusinessException {
        XaResult<List<ShopVo>> xr = new XaResult<List<ShopVo>>();
        Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, "[{property:'sort',direction:'ASC'},{property:'clickCont',direction:'DESC'},{property:'createTime',direction:'DESC'}]");
        Map<String, Object> filterParams = WebUitl.getParametersStartingWith("{}", "search_");
        if (XaUtil.isNotEmpty(categoryType) && XaUtil.isNotEmpty(searchKey)) {
            filterParams = WebUitl.getParametersStartingWith(
                    "{'search_EQ_categoryType':'"+ categoryType+"'" +
                            ",'search_LIKEIGNORE_shopProductName':'"+searchKey+"'}", "search_");
        } else if (XaUtil.isNotEmpty(categoryType) && XaUtil.isEmpty(searchKey)) {
            filterParams =  WebUitl.getParametersStartingWith("{'search_EQ_categoryType':'" + categoryType + "'}", "search_");
        } else if (XaUtil.isEmpty(categoryType) && XaUtil.isNotEmpty(searchKey)) {
            filterParams =  WebUitl.getParametersStartingWith("{'search_LIKEIGNORE_shopProductName':'" + searchKey + "'}", "search_");
        }
        xr =  shopService.findListEQStatusByFilter(XaConstant.Status.publish, filterParams, pageable);
        return xr;
    }


    /**
     * @return
     * @Title: findProductTableById
     * @Description: 根据ID查找单条实体
     * //@param modelId
     */
    @ApiOperation(value = "商品详情", notes = "查询商品表详细信息,当返回的code=1时，取出object进行显示,存放为单体对象")
    @ResponseBody
    @RequestMapping(value = "findShopById", method = RequestMethod.POST)
    public XaResult<ShopVo> findShopById(
            @ApiParam("编号,字段名:id,必填") @RequestParam(value = "id") Long id,
            @ApiParam("用户Id,字段名:userId") @RequestParam(value = "userId", required = false) Long userId
    ) throws BusinessException {
        XaResult<ShopVo> xr = new XaResult<ShopVo>();
        if (XaUtil.isEmpty(id)) {
            xr.error("商品id不能为空！");
            return xr;
        }
        return shopService.findShopDetail(id, userId);
    }
}

